package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.WxUserDao;
import com.leoman.entity.WxUser;
import com.leoman.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wangbin on 2015/7/8.
 */
@Service
@Transactional(readOnly = true)
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    private WxUserDao wxUserDao;

    @Override
    public List<WxUser> findAll() {
        return wxUserDao.findAll();
    }

    @Override
    public Page<WxUser> find(Integer pageNum, Integer pageSize) {
        return wxUserDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<WxUser> find(Integer pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public WxUser getById(Integer id) {
        return wxUserDao.findOne(id);
    }

    @Override
    public WxUser deleteById(Integer id) {
        WxUser wxUser = getById(id);
        wxUserDao.delete(wxUser);
        return wxUser;
    }

    @Override
    public WxUser create(WxUser wxUser) {
        return wxUserDao.save(wxUser);
    }

    @Override
    public WxUser update(WxUser wxUser) {
        return wxUserDao.save(wxUser);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }
}